﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using NetSysData;
using System.Web.UI.HtmlControls;

namespace BladeTecWebApplication.Admin
{
	public partial class EditQuestion : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				NetSysData.NetSysData data = new NetSysData.NetSysData();
				int question_id, i;
				QUESTION question;
				QUESTION_LEVEL[] levels;
				QUESTION_CATEGORY[] categories;

				pnlSuccessMessage.Visible = false;
				try
				{
					question_id = Int32.Parse(Request.QueryString["question_id"]);
				}
				catch (Exception)
				{
					// show error
					return;
				}

				if (Request.QueryString["question_id"] != "")
				{
					question = data.GetQuestion(question_id);
					if (question == null)
					{
						// show error
						return;
					}
				}
				else
				{
					// show error
					return;
				}

				levels = data.GetQuestionLevels();
				categories = data.GetQuestionCagetories();

				ddlLevel.Items.Add(new ListItem("Select ...", ""));
				ddlCategory.Items.Add(new ListItem("Select ...", ""));

				foreach (QUESTION_LEVEL level in levels)
				{
					ddlLevel.Items.Add(new ListItem(level.LEVEL_DESCRIPTION, level.LEVEL_ID.ToString()));
				}

				foreach (QUESTION_CATEGORY category in categories)
				{
					ddlCategory.Items.Add(new ListItem(category.CATEGORY_NAME, category.CATEGORY_ID.ToString()));
				}
				
				rblQuestionType.SelectedIndex = 0;

				txtQuestionText.Text = question.QUESTION_TEXT;
				ddlCategory.SelectedValue = question.QUESTION_CATEGORY.CATEGORY_ID.ToString();
				ddlLevel.SelectedValue = question.QUESTION_LEVEL.LEVEL_ID.ToString();
				rblQuestionType.SelectedValue = question.QUESTION_TYPE;

				//'<tr><td valign="top" style="width:10px;">' + char + ':</td>
				//<td style="width: 10px;">&nbsp;</td>
				//<td><input type="text" name="answer_'+char+'" id="answer_' + char + '" class="stdTextBox" itype="answer" style="width:220px;" /></td>
				//<td style="padding-left: 10px;"><input type="radio" name="correct_answer"'+char+' value="'+char+'" /></td>
				//</tr><tr><td colspan="4" style="height:5px;">&nbsp;</td></tr>';

				// Add answers
				if (question.QUESTION_TYPE == "M")
				{
					i = 0;
					foreach (ANSWER answer in question.ANSWERs)
					{
						char c = (char)(i + 97);

						HtmlTableRow row = new HtmlTableRow();
						HtmlTableCell cell1 = new HtmlTableCell();
						HtmlTableCell cell2 = new HtmlTableCell();
						HtmlTableCell cell3 = new HtmlTableCell();
						HtmlTableCell cell4 = new HtmlTableCell();

						cell1.Align = "top";
						cell1.Style.Add("width", "10px");
						cell1.InnerHtml = c + ":";
						row.Cells.Add(cell1);

						cell2.Style.Add("width", "10px");
						cell2.InnerHtml = "&nbsp;";
						row.Cells.Add(cell2);

						cell3.InnerHtml = "<input type=\"text\" value=\"" + answer.ANSWER_TEXT + "\" name=\"answer_" + c + "\" id=\"answer_" + c + "\" class=\"stdTextBox\" itype=\"answer\" style=\"width:220px;\" />";
						row.Cells.Add(cell3);

						string checkedString = (answer.IS_CORRECT_ANSWER == 1) ? "checked=\"true\"" : "";
						cell4.Style.Add("padding-left", "10px");
						cell4.InnerHtml = "<input type=\"radio\" name=\"correct_answer\" value=\"" + c + "\" " + checkedString + " />";
						row.Cells.Add(cell4);

						answerTable.Rows.Add(row);

						row = new HtmlTableRow();
						cell1 = new HtmlTableCell();
						cell1.ColSpan = 4;
						cell1.Style.Add("height", "5px");
						cell1.InnerHtml = "&nbsp;";
						row.Cells.Add(cell1);

						answerTable.Rows.Add(row);

						i++;
					}
				}
				else
				{
					//pnlAnswerForm.Visible = false;
				}
			}
		}

		protected void btnSaveQuestion_Click(object sender, EventArgs e)
		{
			NetSysData.NetSysData data = new NetSysData.NetSysData();
			QUESTION question;
			int question_id;

			try
			{
				question_id = Int32.Parse(Request.QueryString["question_id"]);
			}
			catch (Exception)
			{
				// show error
				return;
			}

			if (Request.QueryString["question_id"] != "")
			{
				question = data.GetQuestion(question_id);
				if (question == null)
				{
					// show error
					return;
				}
			}
			else
			{
				// show error
				return;
			}

			question.QUESTION_TEXT = txtQuestionText.Text;
			question.QUESTION_TYPE = rblQuestionType.SelectedValue;
			question.LEVEL_ID = Int32.Parse(ddlLevel.SelectedValue);
			question.CATEGORY_ID = Int32.Parse(ddlCategory.SelectedValue);

			if (rblQuestionType.SelectedValue == "M")
			{
				char correct = Request.Form["correct_answer"].ToArray()[0];
				for (int i = 0; i < 10; i++)
				{
					char c = (char)(i + 97);
					string answerText = Request.Form["answer_" + c];

					if (answerText == null)
					{
						continue;
					}

					if (answerText == "")
					{
						pnlErrorEmptyAnswer.Visible = true;
						return;
					}

					ANSWER answer = new ANSWER();
					answer.ANSWER_TEXT = answerText;
					answer.ANSWER_PRIORITY = 0;

					if (c == correct)
					{
						answer.IS_CORRECT_ANSWER = 1;
					}
					else
					{
						answer.IS_CORRECT_ANSWER = 0;
					}

					question.ANSWERs.Add(answer);

				}
			}

			data.SaveQuestion(question);
			pnlDefineQuestionForm.Visible = false;
			pnlSuccessMessage.Visible = true;

		}

	}
}